package com.github.catvod.spider;


import com.github.catvod.bean.Class;
import com.github.catvod.bean.Filter;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 电影牛
 */
public class MovieCow extends Spider {
    private String siteUrl="https://www.dianyingnn.com/";

    private Map<String, String> getHeaders() {
        Map<String, String> header = new HashMap<>();
        header.put("User-Agent", Util.CHROME);
        return header;
    }

    @Override
    public String homeContent(boolean filter) throws Exception {
        List<com.github.catvod.bean.Class> classes = new ArrayList<>();
        LinkedHashMap<String, List<Filter>> filters = new LinkedHashMap<>();
        List<String> typeIds = Arrays.asList("/", "dianying", "dianshiju","zongyi","dongman","wwdj","ysjs","actor");
        List<String> typeNames = Arrays.asList("首页", "电影","电视剧", "综艺","动漫","网文短剧","电影解说","明星");
        for (int i = 0; i < typeIds.size(); i++) classes.add(new Class(typeIds.get(i), typeNames.get(i)));
        return Result.string(classes, filters);
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        if (tid.equals("/")){
            // 首页
            Document doc = Jsoup.parse(OkHttp.string(siteUrl, getHeaders()));
            List<Vod> list=parseCatagoryHtmlVod(doc);
            return Result.string(Integer.parseInt(pg),Integer.parseInt(pg),20,list.size(),list);
        }
        return super.categoryContent(tid, pg, filter, extend);
    }

    private List<Vod> parseCatagoryHtmlVod(Document doc) {
        Elements items = doc.select("ul.fed-list-info > li");
        List<Vod> list = new ArrayList<>();
        for (Element item : items) {
            String pic = item.select("a").first().attr("data-original");
            String id = item.select("a").first().attr("href");
            String title = item.select("a.fed-list-title").text();
            String remark = item.select("span.fed-list-remarks").text();
            list.add(new Vod(id, title, pic, remark));
        }
        return list;
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {

        return super.detailContent(ids);
    }
}
